//*************************************************************************************************
//Copyright (c) 2015.year. @hjjer.
//If you are a hjjer(hejianjiao.org), This code is unlicensed, otherwise,is licensed under the GPL.
//*************************************************************************************************

package com.truthbean.ssm.core.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Blob;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Truthbean
 * @since 2015-2-17 17:03:45
 */
public class StringUtil {

    public static boolean isEmpty(Object obj) {
        return obj != null && obj instanceof String && ((String) obj).isEmpty();
    }

    public static boolean isRealValue(Object obj) {
        return obj != null && obj instanceof String && !((String) obj).trim().isEmpty();
    }

    public static boolean isNotEmpty(Object obj) {
        return obj != null && obj instanceof String && !((String) obj).isEmpty();
    }

    public static boolean isNotEmpty(Object... args) {
        int count = 0;
        if (args != null) {
            for (Object obj : args) {
                if (obj != null && obj instanceof String
                        && !((String) obj).trim().isEmpty()) {
                    count++;
                } else {
                    break;
                }
            }
            return count == args.length;
        }
        return false;
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }

    public static String generateHtmlURI() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");// 设置日期格式
        StringBuilder result = new StringBuilder(df.format(new Date(System
                .currentTimeMillis())));// new Date()为获取当前系统时间
        result.append(".html");
        return result.toString();
    }

    public static String format(Object obj) {
        String result = null;
        if (obj != null && obj instanceof Blob) {
            Blob blob = (Blob) obj;
            int bolblen = 0;
            try {
                bolblen = (int) blob.length();
            } catch (SQLException e) {
                Logger.getLogger(StringUtil.class.getName()).log(Level.SEVERE,
                        null, e);
            }
            byte[] data = null;
            try {
                data = blob.getBytes(1, bolblen);
            } catch (SQLException e) {
                Logger.getLogger(StringUtil.class.getName()).log(Level.SEVERE,
                        null, e);
            }
            result = new String(data);
        }
        return result;

    }

    public static String format$(Object obj) {
        String result = null;
        if (obj != null && (obj instanceof String || obj instanceof Blob)) {
            InputStream is = null;
            try {
                is = ((Blob) obj).getBinaryStream();
                ByteArrayInputStream bais = (ByteArrayInputStream) is;
                byte[] byte_data = new byte[bais.available()]; // bais.available()返回此输入流的字节数
                bais.read(byte_data, 0, byte_data.length);// 将输入流中的内容读到指定的数
                result = new String(byte_data, "utf-8");
            } catch (SQLException e) {
                Logger.getLogger(StringUtil.class.getName()).log(Level.SEVERE,
                        "转换成字节流异常", e);
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(StringUtil.class.getName()).log(Level.SEVERE,
                        "编码格式转换异常", ex);
            }

            // 再转为String，并使用指定的编码方�?
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                Logger.getLogger(StringUtil.class.getName()).log(Level.SEVERE,
                        "InputStream关闭异常", e);
            }
        }
        return result;
    }

    public static String Html2Text(String inputString) {
        String htmlStr = inputString; // 含html标签的字符串
        String textStr = "";
        Pattern p_html;
        Matcher m_html;

        try {
            String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式

            p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
            m_html = p_html.matcher(htmlStr);
            htmlStr = m_html.replaceAll(""); // 过滤html标签

            textStr = htmlStr.replaceAll("\\s", "").replaceAll("&nbsp;", "")
                    .trim();

        } catch (Exception e) {
            System.err.println("Html2Text: " + e.getMessage());
        }

        return textStr;// 返回文本字符串
    }
}
